<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tester extends CI_Controller {
    
    public function index()
        {
          echo "Tester controller";
        }
        
        public function query1()
          {
            # POINTING TO A TABLE WITHOUT ANY KEY 
           $this->load->library('QUERY');
           
           $member3 = new QUERY(array('TABLE'=>'member'));
          
           echo"<pre>";
           print_r($member3);
           echo"</pre>";
          }
        
        public function query2()
         {
	   # FETCHING ALL COLUMNS WITHIN THE TABLE WHEN THE COLS PARAM IS NOT SPECIFIED
           $this->load->library('QUERY');
           
           $keys2  = array('member_id'=>3);
           
           # CREATE AN INSTANCE
           $member2 = new QUERY(array('TABLE'=>'member','KEY'=>$keys2));
	  
           # LIST ALL VALUES BASED ON THE INSTANCE
           echo"<pre>";
           print_r($member2);
           echo"</pre>";  
           
           unset($member2);
           
         }
        
        public function query3()
	{
         # CREATING AN INSTANCE OF THE QUERY POINTING TO A TABLE WITH SPECIFIC COLUMNS BY PASSING THE PARAM COLS   
	  $this->load->library('QUERY');	
	 
          # PREPARE THE QUERY PARAMS
	  $cols  = array('member_id','name'=>'first_name,last_name','email','birthday');
	  $keys  = array('member_id'=>2);
	  
          # CREATE AN INSTANCE  
	  $member = new QUERY(array('TABLE'=>'member','KEY'=>$keys,'COLS'=>$cols,'DESC'=>'member_id','LIMIT'=>'0,2'));
	 
	 # LIST ALL VALUES BASED ON THE INSTANCE
          echo"<pre>";
	  print_r($member);
	  echo"</pre>";
          
          unset($member);
        }
        
        public function query4()
          {
            # FETCHING VALUES FROM THE QUERY
           $this->load->library('QUERY');
           
          // $cols  = array('member_id','name'=>'first_name,last_name','email','birthday');
	   $keys  = array('address'=>'');
           $member4 = new QUERY(array('TABLE'=>'member','KEY'=>$keys));
           
          # GET NUMBER OF ROWS
           echo '<p>COUNT: '.$member4->numRows().'</p>';
           
          # TO FETCH INDIVIDUAL FIELD.
           echo "FETCH NAME: " . $member4->fetch('first_name');
	  
          # IF THE RESULT OF THE QUERY IS MORE THAN ONE ROW, FUNCTION FETCH WILL RETURN THE FIRST ROW 
	  # PASS A 2ND PARAM (INDEX) TO SPECIFY THE PLACEMENT ON THE ARRAY TO FETCH
	   echo'<br />';
           echo "FETCH NAME ON 2ND ROW: " . $member4->fetch('first_name',1);
            
          
          # OR USE THE FUNCTION FETCH ROW WHICH RETUNS THE ENTIRE FIRST ROW (OR INDICATE PLACEMENT)
           echo'<hr />FIRST ROW RESULTS: ';
           $row = $member4->fetchRow();
           echo'<pre>';
           print_r($row);
           echo'</pre>';
           
             
          # OR USE THE FUNCTION FETCH ALL WHICH RETUNS AN ARRAY OF ALL THE RESULTS
           echo'<hr />ALL RESULTS: ';
           $all = $member4->fetchAll();
           echo'<pre>';
           print_r($all);
           echo'</pre>';
	 
           
           unset($member4);
	}
        
        public function query5()
        {
          $this->load->library('QUERY');   
         # SAVING AND UPDATING COLUMNS ON THE TABLE 
         # IF THE QUERY IS POINTED TO ONE (OR MORE) ROW(S), CALLING FUNCTION SAVE WILL AUTOMATICALLY UPDATE THE ROW(S)
         # WARNING : IF YOU INTEND TO UPDATE A ROW, MAKE SURE THAT YOU ARE POINTED TO ONLY ONE INSTANCE, AS EXECUTING FUCTION
         #           SAVE WILL UPDATE ALL ROWS THE QUERY IS POINTING TO.
         
         # CREATE THE INSTANCE  
         $member = new QUERY(array('TABLE'=>'member','KEY'=>array('member_id'=>3))); 
         
         # PREPARE THE DATA
         $data = array('first_name'=>'updated','last_name'=>'value','email'=>'updated@yahoo.com');
         
         # EXECUTE THE UPDATE
         $result = $member->save($data);
         echo $result ? 'UPDATED' : 'ERROR'; 
        }
        public function query6()
        {
            $this->load->library('QUERY');   
         # IN ORDER TO EXECUTE AN INSERT, DO NOT POINT TO ANY ROW BY NOT SPECIFYING ANY KEY ON THE PARAM
         $member2 = new QUERY(array('TABLE'=>'member'));
         
         $data2 = array('first_name'=>'new','last_name'=>'value','email'=>'new@yahoo.com','photo'=>'user_icon.jpg');
         
         # EXECUTE THE INSERT
         $member2->save($data2);
         
         # CALL THE LAST INSERT ID FUNCTION
         echo 'LAST INSERT ID: ' . $member2->lastId(); // */
         
        }
        
        public function look()
         {
            //echo "here!";
           $this->load->view('looker'); 
         }
}
?>